package com.evaluation.controller;

import com.evaluation.entity.IndexTableEntity;
import com.evaluation.entity.IndexTableEntityExample;
import com.evaluation.mapper.IndexTableEntityMapper;
import com.evaluation.utils.Layui;
import com.evaluation.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/first")
public class IndexController {

    @Autowired
    private IndexTableEntityMapper indexTableEntityMapper;


    /**
     * 添加指标操作
     * @param indexTable
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public Message add(@RequestBody IndexTableEntity indexTable,HttpServletRequest request) {
        HttpSession session=request.getSession();
        String loginUser = session.getAttribute("loginUserType").toString();
        if (loginUser.equals("1")) {
            if (StringUtils.isEmpty(indexTable.getIndexname())) {
                return Message.error("请填写指标名称");
            }
            if (StringUtils.isEmpty(indexTable.getWeight())) {
                return Message.error("请填写指标权重");
            }
            if (StringUtils.isEmpty(indexTable.getParentId())) {
                indexTable.setParentId(null);
            }
            IndexTableEntityExample example = new IndexTableEntityExample();
            example.or().andIndexnameEqualTo(indexTable.getIndexname());
            if (indexTableEntityMapper.selectOneByExample(example) != null) {
                return Message.error("该指标名称已存在");
            }
            if (indexTableEntityMapper.insert(indexTable) <= 0) {
                return Message.error("指标添加失败");
            }
            return Message.success("指标添加成功");
        }
         return Message.error("无权限");
    }

    /**
     * 删除指标操作
     * @param indexTablb
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/delete", consumes = "application/json")
    public Message delete(@RequestBody IndexTableEntity indexTablb,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String loginUser = session.getAttribute("loginUserType").toString();
        if (loginUser.equals("1")) {
            try {
                indexTableEntityMapper.deleteByPrimaryKey(indexTablb.getId());
            } catch (Exception e) {
                return Message.error("删除失败,请联系管理员");
            }
            return Message.success("删除成功");
        }
        return Message.error("无权限");
    }

    /**
     * 编辑指标操作
     * @param indexTable
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    public Message edit(@RequestBody IndexTableEntity indexTable,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String loginUser = session.getAttribute("loginUserType").toString();
        if (loginUser.equals("1")) {
            if (StringUtils.isEmpty(indexTable.getIndexname())) {
                return Message.error("请填写指标名称");
            }
            IndexTableEntity index=indexTableEntityMapper.getCountWeght(indexTable.getIndexname());

            Double s=Double.parseDouble(index.getWeight());
            Double m=Double.parseDouble(indexTable.getWeight());

            if((m+s)>1.0){
                return Message.error("指标权重不能超过1");

            }
            if (StringUtils.isEmpty(indexTable.getWeight())) {

                return Message.error("请填写指标权重");
            }

            if (indexTable.getParentId() == "") {
                indexTable.setParentId(null);
            }
            IndexTableEntityExample example = new IndexTableEntityExample();
            example.or().andIndexnameEqualTo(indexTable.getIndexname());
            IndexTableEntity indexTableEntity = indexTableEntityMapper.selectOneByExample(example);
            if (indexTableEntity != null) {
                if (!indexTable.getId().equals(indexTableEntity.getId())) {
                    return Message.error("该指标名称已存在");
                }
            }
            if (indexTableEntityMapper.updateByPrimaryKey(indexTable) <= 0) {
                return Message.error("指标编辑失败");
            }
            return Message.success("指标编辑成功");
        }
        return Message.error("无权限");
    }

    @RequestMapping("/select")
    public Layui select(@RequestParam(required = false) String indexname, @RequestParam(value = "page") Integer page,
                        @RequestParam(value = "limit") Integer limit) {
        IndexTableEntityExample example = new IndexTableEntityExample();
        IndexTableEntityExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(indexname)) {
            criteria.andIndexnameLike("%"+indexname+"%");
        }

        Long cou = indexTableEntityMapper.countByExample(example);

        if(page!=1){
            page=(page-1)*10;
        }else{
            page=page-1;
        }
        List<IndexTableEntity> indexTableEntities = indexTableEntityMapper.selectByExamplePaging(example, page, limit);
        return Layui.data(cou.intValue(), indexTableEntities);
    }

    @RequestMapping("/selectFir")
    public List<IndexTableEntity> selectFir() {
        IndexTableEntityExample example = new IndexTableEntityExample();
        example.or().andParentIdIsNull();
        return indexTableEntityMapper.selectByExample(example);
    }

    @RequestMapping("/selectList")
    public List<IndexTableEntity> selectList() {
        IndexTableEntityExample example = new IndexTableEntityExample();
        example.or().andParentIdIsNotNull();
        return indexTableEntityMapper.selectByExample(example);
    }

    @RequestMapping("/getFirst")
    public IndexTableEntity getAdmin(@RequestParam(value = "id") Integer indexId) {
        return indexTableEntityMapper.selectByPrimaryKey(indexId);
    }
}
